<?php
namespace api\user\controller;

use api\common\controller\RestUserBaseController;
use think\Db;

class GetcoinController extends RestUserBaseController
{
    /**
     * 第三方充币
     */
    public function recharge(){
        $trade = Db::name('block_chain_trade')
            ->where(["status"=>2,"type"=>1,'coin_type'=>'usdt'])
            ->where('trad_hash!=""')
            ->where("user_id!=0")
            ->limit(50)
            ->select();
        file_put_contents('mytest.txt',$trade,FILE_APPEND );
        if(empty($trade)){
            $this->success('无订单');
        }
        Db::startTrans();
        foreach($trade as $k=>$v){
            if($v['amount']<0){
                continue;
            }
            if(!empty($v['trad_hash'])){
                $userinfo = Db::name('user')->where('id',$v['user_id'])->lock(true)->find();
                $result = Db::name('block_chain_trade')->where(['id' => $v['id']])->update(['status' => 1]);
                if (!$result) {
                    Db::rollback();
                    exit('处理失败');
                }
                $result = Db::name('user')->where('id',$v['user_id'])->setInc('usdt_wallet',$v['amount']);
                if (!$result) {
                    Db::rollback();
                    exit('处理失败');
                }
                $result = Db::name('user_balance_log')->insert([
                    'user_id'=>$v['user_id'],
                    'amount' => $v['amount'],
                    'before' => $userinfo['usdt_wallet'],
                    'after' => $userinfo['usdt_wallet'] + $v['amount'],
                    'type' => 1,
                    'remark' => '公链充值USDT',
                    'source' => 9,
                    'log_type'=>4,
                    'createtime' => time()
                ]);
                if (!$result) {
                    Db::rollback();
                    exit('处理失败');
                }
            }
        }
    }


}
